package com.xk857.simpleeffect;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.igexin.sdk.PushManager;
import com.jpeng.jptabbar.JPTabBar;
import com.jpeng.jptabbar.anno.NorIcons;
import com.jpeng.jptabbar.anno.SeleIcons;
import com.jpeng.jptabbar.anno.Titles;
import com.xk857.simpleeffect.activity.LoginActivity;
import com.xk857.simpleeffect.adapter.MainPagerAdapter;
import com.xk857.simpleeffect.fragment.main.PersonFragment;
import com.xk857.simpleeffect.fragment.main.ProjectFragment;
import com.xk857.simpleeffect.fragment.main.ToDoListFragment;
import com.xk857.simpleeffect.fragment.main.TodayFragment;

import cn.hutool.core.util.StrUtil;

public class MainActivity extends AppCompatActivity {

    @Titles
    private static final String[] mTitles = {"今日待办", "待办事项", "我的项目", "个人中心"};

    @SeleIcons
    private static final int[] mSelectorIcons = {R.drawable.main_bottom1_select, R.drawable.main_bottom2_select, R.drawable.main_bottom3_select, R.drawable.main_bottom4_select};

    @NorIcons
    private static final int[] mNormalIcons = {R.drawable.main_bottom1, R.drawable.main_bottom2, R.drawable.main_bottom3, R.drawable.main_bottom4};

    private final Fragment[] fragmentList = new Fragment[mTitles.length];


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.main_bg));
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        setContentView(R.layout.activity_main);

        // 初始化推送
        PushManager.getInstance().initialize(this);
        PushManager.getInstance().setDebugLogger(this, s -> Log.d("MYTAG", "log:" + s));
        isLogin();
        initView();
    }

    private void initView() {
        JPTabBar mTabBar = findViewById(R.id.tabbar);
        ViewPager viewPager = findViewById(R.id.view_pager);
        mTabBar.setContainer(viewPager);
        try {
            fragmentList[0] = TodayFragment.class.newInstance();
            fragmentList[1] = ToDoListFragment.class.newInstance();
            fragmentList[2] = ProjectFragment.class.newInstance();
            fragmentList[3] = PersonFragment.class.newInstance();
        } catch (IllegalAccessException | InstantiationException e) {
            throw new RuntimeException(e);
        }
        MainPagerAdapter pagerAdapter = new MainPagerAdapter(getSupportFragmentManager(), fragmentList);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOffscreenPageLimit(2);

        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//        mTabBar.showBadge(2, "", true); // 设置底部提示信息
    }

    /**
     * 判断是否登录，如果没有登录进行页面跳转
     */
    private void isLogin() {
        SharedPreferences preferences = this.getSharedPreferences("userInfo", MODE_PRIVATE);
        String token = preferences.getString("token", "");
        if (StrUtil.hasBlank(token)) {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }
    }
}